package com.example.springrabbitmq.controller;

import com.example.springrabbitmq.constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Resource(name = "rabbitTemplate")
    private RabbitTemplate rabbitTemplate;
    @Resource(name = "confirmTemplate")
    private RabbitTemplate confirmTemplate;
    @RequestMapping("simple")
    public String simple() {
        for (int i = 0; i < 20; i++) {
            String message = "simple... " + i;
            rabbitTemplate.convertAndSend("", Constants.SIMPLE_QUEUE, message);
        }
        return "OK";
    }

    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 20; i++) {
            String message = "work... " + i;
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, message);
        }
        return "OK";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        for (int i = 0; i < 20; i++) {
            String message = "fanout ... " + i;
            rabbitTemplate.convertAndSend(Constants.PUBLISH_CHANGE, "", message);
        }
        return "OK";
    }

    @RequestMapping("/routing/{routingKey}")
    public String direct(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.ROUTING_CHANGE, routingKey, "routing test... " +routingKey);
        return "OK";
    }

    @RequestMapping("/topics/{topicsKey}")
    public String topics(@PathVariable("topicsKey") String topicsKey) {
        rabbitTemplate.convertAndSend(Constants.TOPICS_CHANGE, topicsKey, "topics test... " +topicsKey);
        return "OK";
    }

    // 消息确认
    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE, "ack", "ack test...");
        return "ack";
    }

    // 发送方确认
    @RequestMapping("/confirm")
    public String confirm() {
        // 发送消息
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        confirmTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE , "confirm111", "confirm test...", correlationData);
        return "发送成功";
    }

    // 持久化
    @RequestMapping("/persistence")
    public String pres() {
        // 消息持久化
        String message = "persistent test...";
        Message messageObject = new Message(message.getBytes(), new MessageProperties());
        // 持久化
        messageObject.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
//        // 非持久化
        messageObject.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        rabbitTemplate.convertAndSend(Constants.PERSISTENCE_EXCHANGE, "persistence", messageObject);
//        rabbitTemplate.convertAndSend(Constants.PERSISTENCE_EXCHANGE, "persistence", message);
        return "发送成功";
    }

    // 重试
    @RequestMapping("/retry")
    public String retry() {
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE, "retry", "retry test...");
        return "发送成功";
    }

    // ttl
    @RequestMapping("/ttl2")
    public String ttl2() {
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test 30s...", message -> {
            // 消息过期时间为 30 s
            message.getMessageProperties().setExpiration("30000");
            return message;
        });
//        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test 10s ...", message -> {
//            // 消息过期时间为 10 s
//            message.getMessageProperties().setExpiration("10000");
//            return message;
//        });
        return "发送成功";
    }

    @RequestMapping("/ttl")
    public String ttl() {
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE, "ttl", "ttl test...");
        return "发送成功";
    }

    // 死信队列
    @RequestMapping("/dlx")
    public String dlx() {
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE, "normal", "dlx test..." + i);
        }
        return "发送成功";
    }
}
